package com.ktjy.car.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.car.entity.Complaint;
import com.ktjy.car.entity.vo.R;
import com.ktjy.car.service.ComplaintService;
import jakarta.annotation.Resource;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/api")
public class ComplaintController {
    @Resource
    ComplaintService complaintService;

    @GetMapping("/page")
    public R selectPage(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam(required = false) Long brandId,
            @RequestParam(required = false) String status) {
        try {
            Complaint complaint = new Complaint();
            complaint.setBrandId(brandId);
            complaint.setStatus(status);
            
            IPage<Complaint> page = complaintService.findPageVo(complaint, pageNo, pageSize);
            return R.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("获取记录失败: " + e.getMessage());
        }
    }

    @GetMapping("/selectByName")
    public R selectByName(
            @RequestParam(required = false) String brandName,
            @RequestParam(required = false) String problem,
            @RequestParam(required = false) String carType,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size) {
        try {
            // 处理空字符串
            brandName = brandName == null ? "" : brandName.trim();
            problem = problem == null ? "" : problem.trim();
            carType = carType == null ? "" : carType.trim();

            IPage<Complaint> page = complaintService.selectPageByName(brandName, problem, carType, current, size);
            return R.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("搜索失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable("id") Integer id) {
        try {
            int result = complaintService.delete(id);
            if (result > 0) {
                return R.ok("删除成功");
            }
            return R.fail("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("删除失败: " + e.getMessage());
        }
    }

    @PostMapping("/add")
    public R add(@RequestBody Complaint complaint) {
        try {
            // 设置默认状态为0（信息审核）
            complaint.setStatus("0");
            // 设置投诉时间为当前时间
            complaint.setComplaintsTime(LocalDate.now());
            
            int result = complaintService.insert(complaint);
            if (result > 0) {
                return R.ok("添加成功");
            }
            return R.fail("添加失败");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("添加失败: " + e.getMessage());
        }
    }

    @PutMapping("/update/status/{id}")
    public R updateStatus(@PathVariable("id") Long id) {
        try {
            Complaint complaint = new Complaint();
            complaint.setId(id);
            complaint.setStatus("1"); // 设置为厂家受理状态
            
            int result = complaintService.update(complaint);
            if (result > 0) {
                return R.ok("状态更新成功");
            }
            return R.fail("状态更新失败");
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("状态更新失败: " + e.getMessage());
        }
    }
}